/**
 * @(#)ExtraParamOssUploadTask.java 2017年11月2日
 * Copyright(C) 2017年11月2日 ZHEXIN IT CO.,LTD. All rights reserved.
 */
package org.jsbd.boss.task;


import org.apache.commons.lang.StringUtils;
import org.jsbd.boss.service.impl.TaskUtil;
import org.jsbd.boss.util.AesUtils;
import org.jsbd.boss.util.OssUtils;
import org.jsbd.boss.util.ZipUtil;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.io.ByteArrayInputStream;
import java.util.List;
import java.util.Map;
import java.util.concurrent.BlockingQueue;
import java.util.concurrent.LinkedBlockingQueue;
import java.util.concurrent.atomic.AtomicInteger;

/**
 * 额外参数oss上传
 *
 * @author <a href="mailto:zhouchao@zhexinit.com" >周超</a>
 * @version 1.0.0
 */
public class ExtraParamOssUploadTask extends BaseTask {
    public static final Logger LOGGER = LoggerFactory.getLogger(ExtraParamOssUploadTask.class);

    private static AtomicInteger count = new AtomicInteger();

    private static BlockingQueue<Map<String, String>> dataQueue = new LinkedBlockingQueue<Map<String, String>>(5000);

    public static final String EXT_PARAM_PATH = "ext/param/";

    private static final String BUKET_NAME = "webmeng";

    private static boolean addTaskFlag = false;

    public static void addTask(Map<String, String> dataMap) {
        if (!addTaskFlag) {
            return;
        }

        int total = count.getAndIncrement();
        if (total > 5000) {
            LOGGER.error("ExtraParamOssUploadTask total [{}] is so much", count.decrementAndGet());
            return;
        }
        dataMap.put("filePath", EXT_PARAM_PATH);
        dataMap.put("isNew", "0");
        dataQueue.add(dataMap);
    }

    public static void addNewClientTask(Map<String, String> dataMap) {
        if (!addTaskFlag) {
            return;
        }

        int total = count.getAndIncrement();
        if (total > 5000) {
            LOGGER.error("add client file total [{}] is so much", count.decrementAndGet());
            return;
        }
        dataMap.put("isNew", "1");
        dataQueue.add(dataMap);
    }


    @Override
    public void run() {
        addTaskFlag = true;
        while (true) {
            try {
                Map<String, String> dataMap = dataQueue.take();
                count.decrementAndGet();
                String fileName = "";
                if (dataMap.get("isNew").equals("1")) {
                    String imsi = dataMap.get("imsi");
                    fileName = TaskUtil.getNewUserFileName(imsi);
                    String data = dataMap.get("data");
                    byte[] zipFileDatas = ZipUtil.zipFile(data, imsi);
                    byte[] encryptFileDatas = AesUtils.encryptBigFile(zipFileDatas);
                    OssUtils.uploadFile(BUKET_NAME, fileName, encryptFileDatas);
                } else {
                    String imei = dataMap.get("imei");
                    String data = dataMap.get("data");
                    if (StringUtils.isBlank(imei) || StringUtils.isBlank(data)) {
                        continue;
                    }
                    fileName = dataMap.get("filePath") + imei;
                    OssUtils.uploadFile(BUKET_NAME, fileName, data.getBytes("UTF-8"));
                }
                LOGGER.info("fileName [{}] upload", fileName);
            } catch (Exception e) {
                LOGGER.error("ExtraParamOssUploadTask error", e);
            }
        }
    }

}
